// Please make sure that file system access permission in manifest.json has correct value.

const {executeAsModal} = require("photoshop").core;
const {batchPlay} = require("photoshop").action;
const {localFileSystem: fs} = require("uxp").storage;

async function tokenify(url){
   return fs.createSessionToken(await fs.getEntryWithUrl("file:" + url));
}

async function actionCommands() {
   const result = await batchPlay(
      [
         {
            _obj: "export",
            using: {
               _obj: "SaveForWeb",
               $Op: {
                  _enum: "$SWOp",
                  _value: "$OpSa"
               },
               $DIDr: true,
               in: {
                  _path: await tokenify("D:/Own/Art/UI/psd/"),
                  _kind: "local"
               },
               pathName: "D:\Own\Art\UI\psd\xingbuxing.png",
               format: {
                  _enum: "$IRFm",
                  _value: "$PN24"
               },
               interfaceIconFrameDimmed: false,
               transparency: true,
               $Mtt: false,
               $EICC: false,
               $MttR: 255,
               $MttG: 255,
               $MttB: 255,
               $SHTM: false,
               $SImg: true,
               $SWsl: {
                  _enum: "$STsl",
                  _value: "$SLAl"
               },
               $SWch: {
                  _enum: "$STch",
                  _value: "$CHsR"
               },
               $SWmd: {
                  _enum: "$STmd",
                  _value: "$MDCC"
               },
               $ohXH: false,
               $ohIC: true,
               $ohAA: true,
               $ohQA: true,
               $ohCA: false,
               $ohIZ: true,
               $ohTC: {
                  _enum: "$SToc",
                  _value: "$OC03"
               },
               $ohAC: {
                  _enum: "$SToc",
                  _value: "$OC03"
               },
               $ohIn: -1,
               $ohLE: {
                  _enum: "$STle",
                  _value: "$LE03"
               },
               $ohEn: {
                  _enum: "$STen",
                  _value: "$EN00"
               },
               $olCS: false,
               $olEC: {
                  _enum: "$STst",
                  _value: "$ST00"
               },
               $olWH: {
                  _enum: "$STwh",
                  _value: "$WH01"
               },
               $olSV: {
                  _enum: "$STsp",
                  _value: "$SP04"
               },
               $olSH: {
                  _enum: "$STsp",
                  _value: "$SP04"
               },
               $olNC: [
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC00"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC19"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC28"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC24"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC24"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC24"
                     }
                  }
               ],
               $obIA: false,
               $obIP: "",
               $obCS: {
                  _enum: "$STcs",
                  _value: "$CS01"
               },
               $ovNC: [
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC01"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC20"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC02"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC19"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC06"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC24"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC24"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC24"
                     }
                  },
                  {
                     _obj: "$SCnc",
                     $ncTp: {
                        _enum: "$STnc",
                        _value: "$NC22"
                     }
                  }
               ],
               $ovCM: false,
               $ovCW: true,
               $ovCU: true,
               $ovSF: true,
               $ovCB: true,
               $ovSN: "images"
            },
            _options: {
               dialogOptions: "dontDisplay"
            }
         }
      ],
      {}
   );
}

async function runModalFunction() {
   await executeAsModal(actionCommands, {"commandName": "Action Commands"});
}

await runModalFunction();
